// pages/community/community.js
Page({
  data: {
    posts: [
      {
        id: 1,
        avatar: '',
        nickname: '健康达人',
        time: '2小时前',
        content: '今天尝试了新的低卡食谱，用西兰花、鸡胸肉和蘑菇做的炒饭，味道很棒而且热量很低！大家有什么推荐的低卡食谱吗？',
        images: [],
        likes: 24,
        comments: 8,
        isLiked: false
      },
      {
        id: 2,
        avatar: '',
        nickname: '健身小能手',
        time: '5小时前',
        content: '坚持跑步30天，体重下降了3公斤！运动真的能改变生活。附上今天的跑步路线和数据。',
        images: [],
        likes: 42,
        comments: 15,
        isLiked: true
      },
      {
        id: 3,
        avatar: '',
        nickname: '营养师小王',
        time: '1天前',
        content: '分享一个营养搭配小知识：早餐应该包含蛋白质、碳水化合物和维生素。比如鸡蛋+全麦面包+牛奶+水果，就是一个很好的搭配。',
        images: [],
        likes: 56,
        comments: 12,
        isLiked: false
      }
    ],
    showPostModal: false,
    newPostContent: ''
  },

  onLoad() {
    console.log('健康社区页面加载');
  },

  onShow() {
    // 页面显示时的逻辑
  },

  // 显示发帖弹窗
  showPostModal() {
    this.setData({
      showPostModal: true
    });
  },

  // 隐藏发帖弹窗
  hidePostModal() {
    this.setData({
      showPostModal: false,
      newPostContent: ''
    });
  },

  // 输入帖子内容
  onPostInput(e) {
    const value = e.detail.value;
    this.setData({
      newPostContent: value
    });
  },

  // 发布帖子
  publishPost() {
    if (!this.data.newPostContent.trim()) {
      wx.showToast({
        title: '请输入内容',
        icon: 'error'
      });
      return;
    }

    const newPost = {
      id: this.data.posts.length + 1,
      avatar: '',
      nickname: '我',
      time: '刚刚',
      content: this.data.newPostContent,
      images: [],
      likes: 0,
      comments: 0,
      isLiked: false
    };

    this.setData({
      posts: [newPost, ...this.data.posts],
      showPostModal: false,
      newPostContent: ''
    });

    wx.showToast({
      title: '发布成功',
      icon: 'success'
    });
  },

  // 点赞帖子
  likePost(e) {
    const index = e.currentTarget.dataset.index;
    const posts = this.data.posts;
    posts[index].isLiked = !posts[index].isLiked;
    posts[index].likes += posts[index].isLiked ? 1 : -1;

    this.setData({
      posts: posts
    });
  },

  // 评论帖子
  commentPost(e) {
    const id = e.currentTarget.dataset.id;
    wx.showToast({
      title: '评论功能待开发',
      icon: 'none'
    });
  },

  // 底部导航切换
  switchTab(e) {
    const tab = e.currentTarget.dataset.tab;
    
    switch (tab) {
      case 'home':
        wx.redirectTo({
          url: '/pages/index/index'
        });
        break;
      case 'history':
        wx.redirectTo({
          url: '/pages/history/history'
        });
        break;
      case 'plan':
        wx.redirectTo({
          url: '/pages/plan/plan'
        });
        break;
      case 'profile':
        wx.redirectTo({
          url: '/pages/profile/profile'
        });
        break;
      default:
        wx.redirectTo({
          url: '/pages/index/index'
        });
    }
  }
})